package com.artisan.admin.controllers;

import com.artisan.admin.model.startPageViewModels.StartPageViewModel;
import com.artisan.admin.model.versionViewModels.VersionViewModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.admin.resultModels.startPageResultModels.StartPageResultModel;
import com.artisan.admin.resultModels.versionResultModels.VersionResultModel;
import com.artisan.domain.dto.PageDto;
import com.artisan.domain.dto.StartPageDto;
import com.artisan.domain.dto.VersionDto;
import com.artisan.domain.serviceInterface.IVersionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 版本控制器
 */
@RequestMapping("/api/version")
@RestController
@Api("版本模块")
@ApiOperation(value = "版本模块")
public class VersionController {
    @Autowired
    private IVersionService versionService;

    /**
     * 获得列表
     *
     * @param pageIndex 当前页
     * @param pageSize 每页数
     */
    // GET /list
    @ApiOperation(value = "获得列表")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "platform",
                    dataType = "Int",
                    required = true,
                    value = "平台，1安卓，2iOS",
                    defaultValue = "1"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "number",
                    dataType = "Int",
                    required = true,
                    value = "版本号",
                    defaultValue = "1.1.1"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "pageIndex",
                    dataType = "Int",
                    required = true,
                    value = "当前页，从0开始",
                    defaultValue = "0"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "pageSize",
                    dataType = "Int",
                    required = true,
                    value = "每页数",
                    defaultValue = "10")
    })
    @ApiResponses(@ApiResponse(code = 200, response = VersionResultModel.class, message = "成功"))
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET})
    public MessageBase getList(
            @RequestParam(value = "platform") int platform,
            @RequestParam(value = "number") String number,
            @RequestParam(value = "pageIndex") int pageIndex,
            @RequestParam(value = "pageSize") int pageSize) {
        MessageBase result = new MessageBase();
        PageDto<VersionDto> page = this.versionService.findList(platform, number, pageIndex, pageSize);
        result.setPageIndex(page.getNumber());
        result.setPageSize(page.getSize());
        result.setTotalRecord(Math.toIntExact(page.getTotalElements()));

        ArrayList<VersionResultModel> models = new ArrayList<>();

        page.getContent()
                .forEach(
                        dto -> {
                            VersionResultModel model = new VersionResultModel();
                            model.setId(dto.getId());
                            model.setNumber(dto.getNumber());
                            model.setPlatform(dto.getPlatform());
                            model.setUpdateType(dto.getUpdateType());
                            model.setAddDate(dto.getAddDate());
                            model.setStatus(dto.getStatus());
                            model.setUrl(dto.getUrl());
                            model.setContent(dto.getContent());

                            models.add(model);
                        });

        result.setData(models);
        return result;
    }
    /**
     * 获得详情
     *
     * @param id id
     */
    // GET /{id}
    @ApiOperation(value = "获得详情")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "id",
                    dataType = "Int",
                    required = true,
                    value = "id",
                    defaultValue = "1")
    })
    @ApiResponses(@ApiResponse(code = 200, response = VersionResultModel.class, message = "成功"))
    @RequestMapping(
            value = {"/{id}"},
            method = {RequestMethod.GET})
    public MessageBase get(@PathVariable int id) {
        MessageBase result = new MessageBase();
        VersionDto dto = this.versionService.find(id);

        VersionResultModel model = new VersionResultModel();
        model.setId(dto.getId());
        model.setNumber(dto.getNumber());
        model.setPlatform(dto.getPlatform());
        model.setUpdateType(dto.getUpdateType());
        model.setAddDate(dto.getAddDate());
        model.setStatus(dto.getStatus());
        model.setUrl(dto.getUrl());
        model.setContent(dto.getContent());

        result.setData(model);
        return result;
    }
    /**
     * 修改
     * @return 状态
     */
    // PUT /
    @ApiOperation(value = "修改")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(method = {RequestMethod.PUT})
    public MessageBase put(@RequestBody VersionViewModel model) {
        MessageBase result = new MessageBase();

        VersionDto dto = new VersionDto();
        dto.setId(model.getId());
        dto.setNumber(model.getNumber());
        dto.setPlatform(model.getPlatform());
        dto.setUpdateType(model.getUpdateType());
        dto.setUrl(model.getUrl());
        dto.setContent(model.getContent());

        boolean b = this.versionService.update(dto);

        result.setSuccess(b);

        return result;
    }
    /**
     * 删除
     * @return 状态
     */
    // DELETE /{1}
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "id",
                    dataType = "Int",
                    required = true,
                    value = "id",
                    defaultValue = "1")
    })
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value ={"/{id}"}, method = {RequestMethod.DELETE})
    public MessageBase delete(@PathVariable int id) {
        MessageBase result = new MessageBase();
        boolean b = this.versionService.delete(id);

        result.setSuccess(b);
        return result;
    }
}
